/**
 * 客户管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <!-- <el-select size="small" v-model="formInline.isLock" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="N"></el-option>
          <el-option label="已锁定" value="Y"></el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.ocrUserName" placeholder="输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.ocrUserPhone" placeholder="输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="primary" @click="exportOcrUserList()">导出</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
      border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column label="序号" width="50px" align="center">
        <template slot-scope="scope">
          {{ scope.$index + (formInline.current - 1) * formInline.size + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="ocrUserName" label="姓名" width="120">
      </el-table-column>
      <el-table-column align="center" prop="ocrUserPhone" label="手机号" width="120">
      </el-table-column>
      <el-table-column align="center" prop="province" label="省" min-width="100">
      </el-table-column>
      <el-table-column align="center" prop="city" label="市" min-width="100">
      </el-table-column>
      <el-table-column align="center" prop="county" label="区" min-width="100">
      </el-table-column>
      <el-table-column align="center" prop="village" label="详细地址" min-width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="createTime" label="创建时间" min-width="140">
        <template slot-scope="scope">
          <div>{{ scope.row.createTime | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteCustomerInfo(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini" type="success" @click="resetpwd(scope.$index, scope.row)">复制</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="姓名" prop="ocrUserName">
          <el-input size="small" v-model="editForm.ocrUserName" auto-complete="off" placeholder="请输入客户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="ocrUserPhone">
          <el-input size="small" v-model="editForm.ocrUserPhone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input size="small" v-model="editForm.province" placeholder="请输入省份"></el-input>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input size="small" v-model="editForm.city" placeholder="请输入城市"></el-input>
        </el-form-item>
        <el-form-item label="区" prop="county">
          <el-input size="small" v-model="editForm.county" placeholder="请输入地区"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="village">
          <el-input size="small" v-model="editForm.village" placeholder="请输入详细地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  customerList, updateCustomer, saveCustomer, deleteCustomer, exportCustomer
} from '../../api/EcommerceManagement'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: '添加客户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      // 编辑与添加
      editForm: {
        id: '',
        ocrUserName: '',
        ocrUserPhone: '',
        province: '',
        city: '',
        county: '',
        village: ''
        // token: localStorage.getItem('logintoken')
      },
      // 选择数据
      selectdata: [],
      // rules表单验证
      rules: {
        ocrUserName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        ocrUserPhone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1([3589][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/,
            required: true,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        userSex: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      },
      // 删除用户
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 请求数据参数
      formInline: {
        current: 1,
        size: 10,
        ocrUserName: '',
        ocrUserPhone: '',
      },
      //用户数据
      userData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
      // 分页赋值
      this.pageparm.currentPage = this.formInline.current
      this.pageparm.pageSize = this.formInline.size
      // 列表数据
      customerList(parameter).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.userData = res.records
          // 分页赋值
          this.pageparm.currentPage = this.formInline.current
          this.pageparm.pageSize = this.formInline.size
          this.pageparm.total = res.total
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.current = parm.currentPage
      this.formInline.size = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      this.getdata(this.formInline)
    },
    // 修改type
    editType: function (index, row) {
      this.loading = true
      let parm = {
        lock: '',
        userId: '',
        token: localStorage.getItem('logintoken')
      }
      parm.userId = row.userId
      let lock = row.isLock
      if (lock == 'N') {
        parm.lock = 'Y'
      } else {
        parm.lock = 'N'
      }
      // 修改状态
      userLock(parm).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.$message({
            type: 'success',
            message: '状态修改成功'
          })
          this.getdata(this.formInline)
        }
      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改客户信息'
        this.editForm.id = row.id
        this.editForm.ocrUserName = row.ocrUserName
        this.editForm.ocrUserPhone = row.ocrUserPhone
        this.editForm.province = row.province
        this.editForm.city = row.city
        this.editForm.county = row.county
        this.editForm.village = row.village
      } else {
        this.title = '添加客户'
        this.editForm.id = ''
        this.editForm.ocrUserName = ''
        this.editForm.ocrUserPhone = ''
        this.editForm.province = ''
        this.editForm.city = ''
        this.editForm.county = ''
        this.editForm.village = ''
      }
    },
    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          console.log("-----3333---" + this.editForm.id)
          if (this.editForm.id == undefined || this.editForm.id == "" || this.editForm.id == null) {
            saveCustomer(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('保存失败，请稍后再试！')
              })
             return
          }
            updateCustomer(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '修改成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('保存失败，请稍后再试！')
              })
        } else {
          return false
        }
      })
    },
    //导出
    exportOcrUserList: function () {
      this.loading = true
      // 分页赋值
      let params = {
        current: this.formInline.current,
        size: this.formInline.size
      }
      // 列表数据
      exportCustomer(params).then(res => {
        this.loading = false
        let blob = new Blob([res.data]);
        let fileName = res.headers['content-disposition'].split('filename=').pop();
        fileName = decodeURI(fileName)
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);
        document.body.removeChild(elink);
        this.$message({
          message: "导出成功~",
          type: "success",
        });
      })
    },
    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },
    // 删除用户
    deleteCustomerInfo(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除
        let param = {
          id: row.id
        }
        deleteCustomer(param)
          .then(res => {
            if (res.success) {
              this.$message({
                type: 'success',
                message: '数据已删除!'
              })
              this.getdata(this.formInline)
            } else {
              this.$message({
                type: 'info',
                message: res.msg
              })
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('数据删除失败，请稍后再试！')
          })
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除！'
          })
        })
    },
    // 复制
    resetpwd(index, row) {
      let _this = this;
      this.$copyText(row.ocrUserPhone).then(//这个地方写自己想要复制出的数据
        function (e) {
          _this.$message({
            showClose: true,
            message: "复制成功",
            type: "success",
          });
        },
        function (e) {
          _this.$message({
            showClose: true,
            message: "复制失败，请手动复制",
            type: "error",
          });
        })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

 